/*
 * @Descripttion : 加载层
 * @Author       : QianFox Team
 * @Date         : 2022-03-09 14:05:44
 * @LastEditors  : QianFox Team
 * @LastEditTime : 2022-06-01 09:27:09
 */

import { _zIndex, loadingIcon } from './common/variables';
import { _typeOf } from './common/utils';

/**
 * @description: 添加加载
 * @param {*} el              要覆盖的节点
 * @param {*} text            加载图片下的文字
 * @param {*} info            提示文字
 * @param {*} background      加载图背色    1、white, 2、black;
 * @param {*} type            类型          1、none, 2、block;
 * @param {*} iconColor       图标颜色
 * @param {*} iconType        加载图标类型：1、circular,  2、spinner；可传可不传，不传默认为 circular
 * @param {*} iconSize        图标尺寸
 * @return {*}
 * @Date: 2022-03-09 14:30:53
 */
function Loading({ el, text, info, background, type, iconColor, iconType, iconSize }) {
    this.LOADING = '.foxui-loading';
    this.el = el || 'body';
    this.text = text || '';
    this.info = info || '';
    this.background = background === 'black' ? 'rgba(0,0,0,0.7)' : 'rgba(255, 255, 255, 0.9)';
    this.type = type || 'none';
    this.iconColor = iconColor || '';
    this.iconType = iconType || '';
    this.iconSize = iconSize || '';
}

/**
 * @description: 添加加载层
 * @param {*}
 * @return {*}
 * @Date: 2022-03-09 18:17:19
 */
Loading.prototype._add = function () {
    let that = this,
        $el = $(this.el),
        html = [
            `<div class="foxui-loading ${this.el === 'body' ? 'is-fullscreen' : ''}" style="z-index:${_zIndex()}; display:none; background-color:${this.background}">`,
            `<div class="foxui-loading-inner${this.type === 'block' ? ' block' : ''}">`,
            `${loadingIcon({ color: this.iconColor, type: this.iconType, size: this.iconSize })}`,
            `${this._textHtml()}`,
            '</div>',
            '</div>',
        ].join('');

    $el.each(function () {
        let $this = $(this),
            position = $this.css('position'),
            len = $this.children(that.LOADING).length;

        if (len < 1) {
            $this.append(html);
            $this.children(that.LOADING).fadeIn('fast');

            if (position !== 'relative' && position !== 'absolute' && position !== 'fixed') {
                $this.css('position', 'relative');
            }
        }
    });

    if (this.el === 'body') {
        // 设置 body hidden
        $('body').css('overflow', 'hidden');
    }

    return $el;
};

/**
 * @description: 关闭加载层
 * @param {*}
 * @return {*}
 * @Date: 2022-03-09 18:16:24
 */
Loading.prototype._close = function () {};

/**
 * @description: 清空加载层
 * @param {*}
 * @return {*}
 * @Date: 2022-03-09 23:14:49
 */
Loading.prototype._clear = function () {};

/**
 * @description: 加载层内容 html
 * @param {*}
 * @return {*}
 * @Date: 2022-03-09 18:16:43
 */
Loading.prototype._textHtml = function () {
    let html = '';
    if (this.type === 'block') {
        html = `<h3>${this.text}</h3><p>${this.info}</p>`;
    } else {
        html = `<span>${this.text}</span>`;
    }
    return html;
};

/**
 * @description: 实例化加载层函数
 * @param {*} data
 * @return {*}
 * @Date: 2022-03-09 18:17:00
 */
function loading(data) {
    let params = {};
    if (data) {
        if (_typeOf(data) === 'object') {
            data && (params = data);
        } else {
            throw new Error(`loading 方法传入参数必须是一个对象或不传入参数！`);
        }
    }

    // 实例化加载层
    let loading = new Loading(params);
    // 添加加载层
    return loading._add();
}

/**
 * @description: 关闭 loading
 * @param {*} el       foxui-loading 容器节点集
 * @return {*}
 * @Date: 2022-03-09 21:32:28
 */
function closeLoading(el) {
    el = el || 'body';

    let $el = $(el);

    $el.each(function () {
        $(this)
            .children('.foxui-loading')
            .fadeOut('fast', function () {
                let $this = $(this),
                    isFllscreen = $this.is('.is-fullscreen');

                $this.remove();
                if (isFllscreen) {
                    // 设置 body auto
                    $('body').css('overflow', 'auto');
                }
            });
    });
}

/**
 * @description: 清空所有的 loading
 * @param {*}
 * @return {*}
 * @Date: 2022-03-09 21:43:33
 */
function clearLoading() {
    $('.foxui-loading').fadeOut('fast', function () {
        $(this).remove();
        // 设置 body auto
        $('body').css('overflow', 'auto');
    });
}

export { loading, closeLoading, clearLoading };
